package com.uphone.kingmall.adapter;

import android.app.Activity;
import android.view.View;
import android.widget.CheckBox;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.china.zhuoyue.R;
import com.uphone.kingmall.bean.HistoryGoodsBean;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/22
 *     desc   :
 * </pre>
 */
public class HistoryListAdapter extends BaseQuickAdapter<HistoryGoodsBean.DataBean, BaseViewHolder> {

    private final Activity activity;
    private OnShopItemClickListener listener;
    private boolean manage = false;

    public HistoryListAdapter(Activity activity) {
        super(R.layout.kingmall_item_history_goods);
        this.activity = activity;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final HistoryGoodsBean.DataBean item) {
        helper.setText(R.id.tv_date, item.getFootDate() + "");
        final CheckBox iv_select = helper.getView(R.id.iv_select);
        if (manage) {
            iv_select.setVisibility(View.VISIBLE);
        } else {
            iv_select.setVisibility(View.GONE);
        }
        iv_select.setChecked(item.isSelect());
        RecyclerView rv = helper.getView(R.id.rv);
        rv.setLayoutManager(new GridLayoutManager(mContext, 3));
        final HistoryGoodsItemAdapter adapter = new HistoryGoodsItemAdapter(activity, item.getDateFoots(), manage);
        rv.setAdapter(adapter);
        adapter.setOnGoodsItemClickListener(new HistoryGoodsItemAdapter.OnGoodsItemClickListener() {
            @Override
            public void itemClick(View v, int childPosition, HistoryGoodsBean.DataBean.DateFootsBean childItem) {
                if (listener != null) {
                    listener.itemGoodClick(v, childPosition, childItem);
                }
            }

            @Override
            public void itemSelect(View v, int childPosition, HistoryGoodsBean.DataBean.DateFootsBean childItem) {
                item.getDateFoots().set(childPosition, childItem);
                if (!childItem.isSelect() && item.isSelect()) {
                    item.setSelect(!item.isSelect());
                    iv_select.setChecked(item.isSelect());
                } else {
                    boolean check = true;
                    for (int i = 0; i < item.getDateFoots().size(); i++) {
                        if (!item.getDateFoots().get(i).isSelect()) {
                            check = false;
                            break;
                        }
                    }
                    item.setSelect(check);
                    iv_select.setChecked(item.isSelect());
                }
                if (listener != null) {
                    listener.itemGoodSelect(v, helper.getLayoutPosition(), childPosition, item, childItem);
                }
            }
        });

        iv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = iv_select.isChecked();
                item.setSelect(isChecked);
                for (int i = 0; i < item.getDateFoots().size(); i++) {
                    item.getDateFoots().get(i).setSelect(isChecked);
                    adapter.setNewData(item.getDateFoots());
                }
                if (listener != null) {
                    listener.itemSelect(iv_select, helper.getLayoutPosition(), item);
                }
            }
        });
    }


    public void setOnShopItemClickListener(OnShopItemClickListener listener) {
        this.listener = listener;
    }

    public void setManager(boolean manage) {
        this.manage = manage;
        notifyDataSetChanged();
    }

    public interface OnShopItemClickListener {
        void itemClick(View v, int position, HistoryGoodsBean.DataBean item);

        void itemSelect(View v, int position, HistoryGoodsBean.DataBean item);

        void itemGoodClick(View v, int childPosition, HistoryGoodsBean.DataBean.DateFootsBean childItem);

        void itemGoodSelect(View v, int position, int childPosition, HistoryGoodsBean.DataBean item, HistoryGoodsBean.DataBean.DateFootsBean childItem);
    }
}
